#include "chat_ui.h"
#include <QVBoxLayout>
#include <QDateTime>
#include <QMessageBox>
chat_ui::chat_ui(QWidget *parent) : QWidget{ parent },Client{chat_client::getInstance()}
{
    initLayout();
    connectInit();
}

void chat_ui::initLayout()
{
    this->setFixedSize(800, 800);
    QVBoxLayout *main_lay = new QVBoxLayout;
    main_lay->setContentsMargins(0, 0, 0, 0);
    main_lay->setSpacing(0);
    this->setLayout(main_lay);
    text_browser = new QTextBrowser;
    text_browser->setFixedSize(800, 600);
    QHBoxLayout *txt_lay = new QHBoxLayout;
    QHBoxLayout *ip_lay = new QHBoxLayout;
    QHBoxLayout *option_lay = new QHBoxLayout;
    txt_lay->setContentsMargins(0, 0, 0, 0);
    ip_lay->setContentsMargins(0, 0, 0, 0);
    option_lay->setContentsMargins(0, 0, 0, 0);
    txt_lay->setSpacing(0);
    ip_lay->setSpacing(0);
    option_lay->setSpacing(0);

    ip_lab=new QLabel("聊天群ip:");
    ip_lab->setFixedSize(200,60);
    port_lab=new QLabel("端口:");
    port_lab->setFixedSize(120,60);
    id_lab=new QLabel("昵称:");
    id_lab->setFixedSize(120,60);
    id_line=new QLineEdit;
    id_line->setFixedSize(160,60);
    txt_line=new QLineEdit;
    txt_line->setFixedSize(600,80);
    ip_line=new QLineEdit;
    ip_line->setFixedSize(320,60);
    port_line=new QLineEdit;
    port_line->setFixedSize(160,60);
    send_txt_btn=new QPushButton("发送消息");
    send_txt_btn->setFixedSize(200,80);
    send_file_btn=new QPushButton("传送文件");
    send_file_btn->setFixedSize(120,60);
    connect_btn=new QPushButton("加入群聊");
    connect_btn->setFixedSize(120,60);

    viewFilesBtn=new QPushButton("查看群文件");
    viewFilesBtn->setFixedSize(160,60);
    fileListWidget=new QListWidget();
    fileListWidget->hide();

    txt_lay->addWidget(txt_line);
    txt_lay->addWidget(send_txt_btn);
    ip_lay->addWidget(ip_lab);
    ip_lay->addWidget(ip_line);
    ip_lay->addWidget(port_lab);
    ip_lay->addWidget(port_line);
    option_lay->addWidget(send_file_btn);
    option_lay->addWidget(viewFilesBtn);
    option_lay->addStretch();
    option_lay->addWidget(id_lab);
    option_lay->addWidget(id_line);
    option_lay->addWidget(connect_btn);


    main_lay->addWidget(text_browser);
    main_lay->addLayout(txt_lay);
    main_lay->addLayout(ip_lay);
    main_lay->addLayout(option_lay);


}

void chat_ui::connectInit()
{
    connect(send_txt_btn,&QPushButton::clicked,this,&chat_ui::send_txt_btn_clicked_slot);
    connect(connect_btn,&QPushButton::clicked,this,&chat_ui::connect_btn_clicked_slot);
    connect(Client,&chat_client::newMsg_sig,this,&chat_ui::pritTobrowser);
    connect(Client,&chat_client::onconnectd_sig,this,[=]{
        pritTobrowser("您已连接");
    });
    connect(Client,&chat_client::disconnectd_sig,this,[=]{
        pritTobrowser(("您已断开"));
    });
    connect(viewFilesBtn,&QPushButton::clicked,this,&chat_ui::viewFilesBtnClicked);
    connect(Client, &chat_client::fileListReceived, this, [=](const QStringList &files) {
        fileListWidget->clear();
        fileListWidget->addItems(files);
    });
    connect(fileListWidget, &QListWidget::itemDoubleClicked, this, &chat_ui::fileSelected);
    connect(Client,&chat_client::fileDownloaded,this,&chat_ui::filedownloaded_slot);
}

void chat_ui::viewFilesBtnClicked()
{
    Client->requestFileList();
    fileListWidget->show();
}

void chat_ui::fileSelected()
{
    QListWidgetItem *selectedItem = fileListWidget->currentItem();
    QString text=selectedItem->text();
    if (selectedItem) {
        Client->downloadFile(text);
    }
}

void chat_ui::filedownloaded_slot(const QString &filename, const QString &savepath)
{
    QMessageBox msgBox;
    msgBox.setIcon(QMessageBox::Information);
    msgBox.setWindowTitle("下载完成");
    msgBox.setText(filename + "保存至" + savepath);
    msgBox.setStandardButtons(QMessageBox::Yes);
    msgBox.exec();
}


void chat_ui::pritTobrowser(QString msg)
{
    QString time=QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    text_browser->append(time);
    text_browser->append(msg);
    text_browser->append("  ");
}

void chat_ui::send_txt_btn_clicked_slot()
{
    QString text=txt_line->text();
    QString id=id_line->text();
    Client->sendToserver(id,text);
    txt_line->clear();
}

void chat_ui::send_file_btn_clicked_slot()
{

}

void chat_ui::connect_btn_clicked_slot()
{
    QString ip=ip_line->text();
    int port=port_line->text().toInt();
    //连接
    Client->connetToserver(ip,port);
}
